﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.Linq;
using System.Linq;
using System.Text;
using IntraVision.Web;
using IntraVision.Web.Filter;
using IntraVision.Web.Security;
using Model;

[Security]
public class NotarialFormHistoryController : TableController<NotarialServiceDataContext, NotarialFormHistory>
{
    DateTime _StartDate;
    DateTime _EndDate;

    public NotarialFormHistoryController() 
    {
        _StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        _EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(1).Month, 1).AddDays(-1);
    }

    public override void Default()
    {
        this.List();
    }

    public override void Create()
    {
        return;
    }

    public override void Delete()
    {
        return;
    }

    public override void Save()
    {
        return;
    }

    public override void List()
    {
        InitDatesFromRequest();

        ViewData["months"] = GetMonthList();
        ViewData["years"] = GetYearsList(DateTime.Now.Year, 10);
        ViewData["startdate"] = _StartDate;
        ViewData["enddate"] = _EndDate;

        base.List();
    }

    protected override IQueryable ConvertQueryToResult(NotarialServiceDataContext dc, IQueryable<NotarialFormHistory> query)
    {
        return (from l in query
        select new
        {
            Name = string.Format("{0} - {1}",l.NotarialForm.NotarialFormSerialNumber.Name, l.NotarialForm.FullName),
            NotarialFormId = l.NotarialForm.Id,
            Date = l.Date,
            UserName = (l.UserId == null) ? "" : l.User.LastName + " " + l.User.Name + " " + l.User.PatronymicName,
            OrderId = l.OrderId.HasValue ? l.OrderId.ToString() : string.Empty,
            StatusName = l.StatusId.HasValue ? dc.NotarialFormStatus.First(s => s.Id == l.StatusId).Name : string.Empty,
        });
    }

    protected override void CreateFilterConditions(NotarialServiceDataContext dc)
    {
        Filter = new Filter<NotarialFormHistory>();
        Filter.Conditions.Add(new DateFilterCondition<NotarialFormHistory>("Date", "Дата/время операции"));
        Filter.Conditions.Add(new UserSelectFilterCondition<NotarialFormHistory>("UserId", "Кто", (from u in dc.Users orderby u.LastName select new { Id = u.Id, Name = u.FullName }), -1, "task.ivp/findusers", "user.ivp/select?reset=true"));        
        Filter.Conditions.Add(new NumericFilterCondition<NotarialFormHistory>("OrderId", "Номер заявки", true));
        Filter.Conditions.Add(new SelectFilterCondition<NotarialFormHistory>("StatusId", "Статус бланка", (from s in dc.NotarialFormStatus select new { Id = s.Id, Name = s.Name })));                
    }

    protected override IQueryable<NotarialFormHistory> CustomFilter(NotarialServiceDataContext dc, IQueryable<NotarialFormHistory> query)
    {
        return query.Where(q => q.Date >= _StartDate && q.Date <= _EndDate);
    }

    private void InitDatesFromRequest()
    {
        DateTime bufDateTime;
        if (!String.IsNullOrEmpty(Request["startdate"]) && DateTime.TryParse(Request["startdate"], out bufDateTime))
        {
            _StartDate = bufDateTime;
            ViewData["currentYear"] = _StartDate.Year;
            ViewData["currentMonth"] = _StartDate.Month;
        }
        if (!String.IsNullOrEmpty(Request["enddate"]) && DateTime.TryParse(Request["enddate"], out bufDateTime))
            _EndDate = bufDateTime;
    }

    protected object GetMonthList()
    {
        int count = 12;
        List<object> monthsList = new List<object>(count);
        for (int i = 0; i < count; i++)
            monthsList.Add(new { id = i + 1, name = (DateTime.MinValue.AddMonths(i)).ToString("MMMM") });
        return monthsList;
    }

    protected object GetYearsList(int lastYear, int count)
    {
        List<object> yearList = new List<object>(count + 1);
        for (int i = lastYear; i >= lastYear - count; i--)
            yearList.Add(new { id = i, name = i.ToString() });
        return yearList;
    }

    public override void RegisterTableScripts()
    {
        base.RegisterTableScripts();

        RegisterScript("report/report.js");
    }
}


